package com.wyp.springbootinit.manager;

import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.model.FileInfo;
import com.qiniu.util.Auth;
import com.wyp.springbootinit.constant.FileConstant;
import com.wyp.springbootinit.model.vo.FileVo;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 4413
 */
@Component
public class KeyManager {

    @Resource
    private Auth getAuth;

    public List<String> getKey() {
        Configuration cfg = new Configuration(Region.huabei());
        Auth auth = getAuth;
        BucketManager bucketManager = new BucketManager(auth, cfg);
        String bucket = "bird-init";

        //文件名前缀
        String prefix = "/user_avatar/1638792928714883073";
        //每次迭代的长度限制，最大1000，推荐值 1000
        int limit = 1000;
        //指定目录分隔符，列出所有公共前缀（模拟列出目录效果）。缺省值为空字符串
        String delimiter = "";

        List<String> keyList = new ArrayList<>();
        BucketManager.FileListIterator fileListIterator = bucketManager.createFileListIterator(bucket, prefix, limit, delimiter);
        while (fileListIterator.hasNext()) {
            //处理获取的file list结果
            FileInfo[] items = fileListIterator.next();
            for (FileInfo item : items) {
                System.out.println(FileConstant.COS_HOST + item.key);
                FileVo fileVo = new FileVo();
                fileVo.setFilePath(FileConstant.COS_HOST + item.key);
                keyList.add(FileConstant.COS_HOST + item.key);
            }
        }
        System.out.println(keyList);
        return keyList;
    }
}
